package com.hhss.qishi.mapper.system;

import com.hhss.qishi.entity.system.Role;
import com.hhss.qishi.mapper.CapitalMapper;
import com.hhss.qishi.mapper.system.provider.RoleProvider;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author hhss
 * @date 2019年3月31日 
 */
@Mapper
@Repository
public interface RoleMapper extends CapitalMapper<Role> {
	
	@SelectProvider(type = RoleProvider.class, method = "getListSQL")
	List<Role> getList(@Param("role") Role role);

	/**
	 * 根据用户id获取角色列表
	 * @param id
	 * @return
	 */
	@SelectProvider(type = RoleProvider.class, method = "getListByUserIdSQL")
	@Results({
			@Result(/*id=true,*/column="id",property="id"),
			@Result(column="role_name",property="roleName"),
			@Result(column="description",property="description"),
			@Result(column="id",property="permissions",
					many=@Many(
							select="com.hhss.qishi.mapper.system.PermissionMapper.getListByRoleId",
							fetchType=FetchType.LAZY
					)
			)
	})
	List<Role> getListByUserId(@Param("id") Long id);

	/**
	 * 获取角色并查找相关权限
	 * @param role
	 * @return
	 */
	@SelectProvider(type = RoleProvider.class, method = "getEntitySQL")
	@Results({
        @Result(/*id=true,*/column="id",property="id"),
        @Result(column="role_name",property="roleName"),
        @Result(column="description",property="description"),
        @Result(column="id",property="permissions",
            many=@Many(
                    select="com.hhss.qishi.mapper.system.PermissionMapper.getListByRoleId",
                    fetchType=FetchType.LAZY
            )
        )
    })
	Role getPermissionRoleById(@Param("role") Role role);
}
